package com.hrbu.springboot04.controller;

import com.hrbu.springboot04.bean.User;
import com.hrbu.springboot04.mapper.UserMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: tanyajun
 * @CreateTime: 2021/4/9
 * @Blog: https://tanyajun.top
 */
@Controller
public class SqlController {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @GetMapping("/selectAll")
    @ResponseBody
    public Map selectAll(){
        List<User> userList = jdbcTemplate.query("select * from user", new BeanPropertyRowMapper<>(User.class));
        HashMap<Object, Object> map = new HashMap<>();
        map.put("userList",userList);
        return map;
    }
    @Autowired
    UserMapper userMapper;

    @GetMapping("/select/{id}")
    @ResponseBody
    public Map select(@PathVariable Integer id){
        User user = userMapper.selectById(id);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("user",user);
        return map;
    }
}
